import java.util.Arrays;
//
public class SeqList {
    private int[] elementData;
    private int size;

    public SeqList(){
        this.elementData = new int[10];
    }

    public static void main(String[] args) {
        SeqList seqList = new SeqList();
        seqList.addLast(1);
        seqList.addFirst(2);
        seqList.addIndex(1,3);
        System.out.println(seqList);
        seqList.remove(1);
        System.out.println(seqList);
        seqList.removeValue(1);
        System.out.println(seqList);
    }

    public void addFirst(int data){
        if (size == elementData.length){
            grow();
        }
        for (int i = size-1; i >= 0; i--) {
            elementData[i+1] = elementData[i];
        }
        elementData[0] = data;
        size++;
    }

    public void addLast(int data){
        if (size == elementData.length){
            grow();
        }
        elementData[size] = data;
        size++;
    }

    public void addIndex(int index,int data){
        if(index < 0 || index > size){
            System.err.println("index非法，无法进行插入");
            return;
        }
        if(size == elementData.length){
            grow();
        }
        if (index == 0){
            addFirst(data);
        }else if(index == size){
            addLast(data);
        }else {
            for (int i = size-1; i >= index; i--) {
                elementData[i+1] = elementData[i];
            }
            elementData[index] = data;
            size++;
        }
    }

    //删除索引值处元素
    public void remove(int index){
        if (rangeCheck(index)){
            for (int i = index; i < size; i++) {
                elementData[i] = elementData[i+1];
            }
            size--;
        }
    }

    //按值删除
    public void removeValue(int value){
        for (int i = 0; i < size; i++) {
            while (elementData[i] == value){
                remove(i);
            }
        }
    }

    //修改
    public int set (int index,int newData){
        if(rangeCheck(index)){
            int oldValue = elementData[index];
            elementData[index] = newData;
            return oldValue;
        }
        return -1;
    }

    //内部扩容方法
    private void grow(){
        int oldLength = elementData.length;
        int newLength = oldLength << 1;
        elementData = Arrays.copyOf(elementData,newLength);
    }

    /**
     * 校验当前传入的index是否合法
     * @param index
     */
    private boolean rangeCheck(int index){
        if(index<0 || index>=size){
            System.err.println("当前index非法，index >= 0 && index < size");
            return false;
        }
        return true;
    }

    public int getSize() {
        return size;
    }

    //按索引查找
    public int get(int index){
        if (rangeCheck(index)){
            return elementData[index];
        }
        return -1;
    }

    //按位置查找
    public boolean contains(int data){
        for (int i = 0; i < size; i++) {
            if(elementData[i] == data){
                return true;
            }
        }
        return false;
    }

    public String toString(){
        String ret = "[";
        for (int i = 0; i < size; i++) {
            ret += elementData[i];
            if(i < size - 1){
                ret += ", ";
            }
        }
        ret +="]";
        return ret;
    }

}
